#!/bin/bash
#we set the compiledir to the /Tmp dir to make the test faster by bypassing the nfs network.
date
START=`date +%s`
ARGS=$@
PROFILING=""

if [ "$1" == "--buildbot" ]; then
    shift
    ARGS="Pylearn2 $@"
    COMPILEDIR=/Tmp/lisa_theano_compile_dir_pylearn2
    ROOT_CWD=/Tmp/nightly_build
    FLAGS=compiledir=$COMPILEDIR
    cd ${ROOT_CWD}/Pylearn2
    git rev-parse HEAD
    cd pylearn2/utils
    python setup.py build_ext --inplace
    cd ../..
    cd pylearn2/scripts/tutorials/grbm_smd/
    wget -c http://www.iro.umontreal.ca/~lisa/datasets/cifar10_preprocessed_train.pkl
    cd ../../../..

    cd ..
    PROFILING="--with-coverage --cover-package=theano"
    NOSETESTS=${ROOT_CWD}/Theano/bin/theano-nose
else
    COMPILEDIR=`python -c "import theano;print theano.config.compiledir"`
    NOSETESTS=`python -c "import theano; print theano.__path__[0]"`/../bin/theano-nose
fi

echo "nb element in the compiledir:"
ls ${COMPILEDIR}|wc -l
FLAGS=warn.argmax_pushdown_bug=False,warn.gpusum_01_011_0111_bug=False,warn.sum_sum_bug=False,warn.sum_div_dimshuffle_bug=False,warn.subtensor_merge_bug=False,$FLAGS
# We want to see correctly optimization/shape errors, so make make them raise an
# error.
FLAGS=on_opt_error=raise,$FLAGS
FLAGS=on_shape_error=raise,$FLAGS

export PYTHONPATH=${ROOT_CWD}:$PYTHONPATH

#We disable as it is too slow and it is not as useful for Pylearn2 as for Theano.
#echo "executing nosetests with mode=FAST_COMPILE"
#THEANO_FLAGS=${FLAGS},mode=FAST_COMPILE ${NOSETESTS} ${ARGS}
#echo "nb element in the compiledir:"
#ls ${COMPILEDIR}|wc -l
echo "executing nosetests with mode=FAST_RUN"
THEANO_FLAGS=${FLAGS},mode=FAST_RUN ${NOSETESTS} ${PROFILING} ${ARGS}
echo "nb element in the compiledir:"
ls ${COMPILEDIR}|wc -l
echo "executing nosetests with mode=FAST_RUN,floatX=float32"
THEANO_FLAGS=${FLAGS},mode=FAST_RUN,floatX=float32 ${NOSETESTS} ${ARGS}
echo "nb element in the compiledir:"
ls ${COMPILEDIR}|wc -l

#we change the seed and record it everyday to test different combination. We record it to be able to reproduce bug caused by different seed. We don't want multiple test in DEBUG_MODE each day as this take too long.
seed=$RANDOM
echo "executing nosetests with mode=DEBUG_MODE with seed of the day $seed"
THEANO_FLAGS=${FLAGS},unittests.rseed=$seed,mode=DEBUG_MODE,DebugMode.check_strides=0,DebugMode.patience=3,DebugMode.check_preallocated_output= ${NOSETESTS} ${ARGS}

echo "nb element in the compiledir:"
ls ${COMPILEDIR}|wc -l

echo
END=`date +%s`
python -c "print 'Total test time: %dm %ds'%((${END} - ${START})/60, (${END} - ${START})%60)"
date
